package com.blockmeta.bbs.immodule.fragment;


import android.annotation.SuppressLint;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.FrameLayout;

import com.blockmeta.bbs.businesslibrary.base.fragment.BaseFragment;
import com.blockmeta.bbs.businesslibrary.widget.emoji.EmojiGridView;
import com.blockmeta.bbs.businesslibrary.widget.emoji.EmojiImageView;
import com.blockmeta.bbs.businesslibrary.widget.emoji.EmojiManager;
import com.blockmeta.bbs.businesslibrary.widget.emoji.OnEmojiClickListener;
import com.blockmeta.bbs.businesslibrary.widget.emoji.OnEmojiLongClickListener;
import com.blockmeta.bbs.businesslibrary.widget.emoji.VariantEmojiManager;
import com.blockmeta.bbs.immodule.R;
import com.blockmeta.bbs.immodule.R2;
import com.blockmeta.bbs.immodule.interfaces.OnEmojiButtonClickListener;
import com.blockmeta.bbs.immodule.widget.chatboard.BaseChatBoard;
import com.vanniktech.emoji.VariantEmoji;
import com.vanniktech.emoji.emoji.Emoji;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * A simple {@link Fragment} subclass.
 * emoji界面
 */
public class EmojiFragment extends BaseFragment {

    @BindView(R2.id.container)
    FrameLayout mContainer;

    private VariantEmoji variantEmoji;

    private OnEmojiClickListener onEmojiClickListener;

    private OnEmojiLongClickListener mOnEmojiLongClickListener;

    private OnEmojiButtonClickListener mOnEmojiButtonClickListener;


    @Override
    public View setContentView(LayoutInflater inflater, ViewGroup container) {
        return inflater.inflate(R.layout.fragment_emoji, container, false);
    }

    @Override
    public void initView() {
        @SuppressLint("RestrictedApi")
        List<Fragment> list = getFragmentManager().getFragments();
        for (Fragment fragment : list) {
            if (fragment instanceof BaseChatBoard) {
                onEmojiClickListener = (OnEmojiClickListener) fragment;
                mOnEmojiButtonClickListener = (OnEmojiButtonClickListener) fragment;
                break;
            }
        }
        this.variantEmoji = new VariantEmojiManager(mContext);

        final OnEmojiLongClickListener longClickListener = new OnEmojiLongClickListener() {
            @Override
            public void onEmojiLongClick(@NonNull final EmojiImageView view, @NonNull final Emoji emoji) {
            }
        };

        final OnEmojiClickListener clickListener = new OnEmojiClickListener() {
            @Override
            public void onEmojiClick(@NonNull final EmojiImageView imageView, @NonNull final Emoji emoji) {
                variantEmoji.addVariant(emoji);
                if (onEmojiClickListener != null) {
                    onEmojiClickListener.onEmojiClick(imageView, emoji);
                }
            }
        };

        EmojiGridView view = new EmojiGridView(mContext).init(clickListener, longClickListener,
                EmojiManager.getInstance().getCategories()[0], variantEmoji);
        view.setOverScrollMode(AbsListView.OVER_SCROLL_NEVER);

        mContainer.addView(view);
    }
    @OnClick({R2.id.iv_delete, R2.id.tv_send_content})
    public void onViewClicked(View view) {
        if (mOnEmojiButtonClickListener != null){
            mOnEmojiButtonClickListener.onEmojiButtonClickListener(view);
        }
    }
}
